package position

import "time"
import "gorm.io/gorm"
import "go_part/utils"
//import "fmt"

type PositionDevice struct{
	gorm.Model
	Name string
	LevelLocalID string
	DeviceLocalID string
	CompanyID string
	Phone string 
	Email string 
	DeviceTypeID string
	Identity string
	Longitude string
	Latitude string
	ClientIP string
	ClientPort string
	FromCompany string
	FromBrand string
	VideoID string
	VideoType string
	Salt string
	LoginTime time.Time
	IsLogout bool
	DeviceInfo string
 }

 func (table *PositionDevice) TableName() string {

	return "position_device_basic"
}

func GePositionDeviceList() []*PositionDevice { 
	data := make([]*PositionDevice,10)
	utils.DB.Find(&data)
	// for _,v : = range data {
	// 	fmt.Println(v)
	// }
	return data
}

func FindPositionDeviceByName(name string) PositionDevice {
	user := PositionDevice{}
	utils.DB.Where("name = ?",name).Find(&user)
	return user
}

func FindPositionDeviceByID(id uint) PositionDevice {
	user := PositionDevice{}
	utils.DB.Where("id = ?",id).First(&user)
	return user
}

func FindPositionDeviceByTypeIDAndCompanyID(id string,CompanyID string) []*PositionDevice {
	user := make([]*PositionDevice,100)
	utils.DB.Where("level_local_id = ? and company_id = ?",id,CompanyID).Find(&user)
	return user
}

func CreatePositionDevice(user PositionDevice) *gorm.DB {
	
	return utils.DB.Create(&user)
}

func DeletePositionDevice(user PositionDevice) *gorm.DB {
	
	return utils.DB.Delete(&user)
}

func UpdatePositionDevice(user PositionDevice) *gorm.DB {
	
	return utils.DB.Model(&user).Updates(PositionDevice{Name: user.Name,DeviceTypeID: user.DeviceTypeID,Longitude: user.Longitude, Latitude: user.Latitude,VideoID: user.VideoID ,VideoType: user.VideoType ,FromCompany: user.FromCompany,FromBrand: user.FromBrand, DeviceInfo: user.DeviceInfo})
}